package com.xuchaotong.zk.listener;

import com.xuchaotong.zk.utils.ZookeeperUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 311633
 * @version 1.0
 * @ClassName:
 * @Description:
 * @date 2018/2/6 13:46
 */
public class ZookeeperConnectionListener implements ConnectionStateListener {
    private static final Logger LOG = LoggerFactory.getLogger(ZookeeperConnectionListener.class);

    @Override
    public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) {

        switch (connectionState) {
            case LOST:
                while (true) {
                    try {
                        if (curatorFramework.getZookeeperClient().blockUntilConnectedOrTimedOut()) {
                            ZookeeperUtils.getInstance().setNodeCacheListener("", false);
                            break;
                        }
                    } catch (InterruptedException e) {
                        LOG.error("", e);
                    }
                }
            case CONNECTED:
                LOG.info("the client is connected...");
                break;
            case SUSPENDED:
                break;
            case READ_ONLY:
                break;
            case RECONNECTED:
                break;
            default:
                break;
        }
    }
}
